//
//  AppDelegate.swift
//  hamaton-app
//
//  Created by intyt on 2024/3/19.
//

import UIKit
import ESTabBarController_swift
import IQKeyboardManagerSwift

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        // 设置语言
        setHamatonLanguage()
        UIAppearanceConfig()
        //键盘
        setupGridIQKeyboar()
        // 设置 window
        window = UIWindow.init(frame: UIScreen.main.bounds)
        window?.backgroundColor = .white
        setRootViewController()
        
        return true
    }
    // 键盘相关 IQKeyboardManager
    private func setupGridIQKeyboar() {
        IQKeyboardManager.shared.enable = true
        IQKeyboardManager.shared.enableAutoToolbar = true
        IQKeyboardManager.shared.shouldShowToolbarPlaceholder = false
        IQKeyboardManager.shared.shouldResignOnTouchOutside = true
    }
    private func UIAppearanceConfig() {

        UINavigationBar.appearance().tintColor = Theme.Color.NavigationBar.white
        UINavigationBar.appearance().barTintColor = Theme.Color.Background.main
        UINavigationBar.appearance().isTranslucent = false
        UINavigationBar.appearance().barStyle = .default
        UINavigationBar.appearance().shadowImage = UIImage()
        UINavigationBar.appearance().titleTextAttributes = [
            .font: Theme.Font.size(16, bold: true),
            .foregroundColor: Theme.Color.NavigationBar.white
        ]
        // 解决iOS15 barTintColor设置无效的问题，参考https://developer.apple.com/forums/thread/682420
        if #available(iOS 15.0, *) {
            let newAppearance = UINavigationBarAppearance()
            newAppearance.configureWithOpaqueBackground()
            newAppearance.backgroundColor = Theme.Color.Background.main
            newAppearance.shadowImage = UIImage()
            newAppearance.shadowColor = nil
            newAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: Theme.Color.NavigationBar.white, NSAttributedString.Key.font: Theme.Font.size(16, bold: true)]
            
            UINavigationBar.appearance().standardAppearance = newAppearance
            UINavigationBar.appearance().scrollEdgeAppearance =  UINavigationBar.appearance().standardAppearance
        }
        
        UIBarButtonItem
            .appearance(whenContainedInInstancesOf: [UINavigationBar.self])
            .setTitleTextAttributes([.font: Theme.Font.size(14, bold: false),
                                     .foregroundColor: Theme.Color.NavigationBar.white], for: .normal)
        UIBarButtonItem
            .appearance(whenContainedInInstancesOf: [UINavigationBar.self])
            .setTitleTextAttributes([.font: Theme.Font.size(14, bold: false),
                                     .foregroundColor: Theme.Color.NavigationBar.white], for: .highlighted)

        UIButton.appearance().isExclusiveTouch = true

        if #available(iOS 10, *) {
            UICollectionView.appearance().isPrefetchingEnabled = false
        }
        UIScrollView.appearance().delaysContentTouches = false

    }
}

extension AppDelegate {
    
    private func setHamatonLanguage() {
        //替换Bundle.main为自定义的MyBundle
        object_setClass(Foundation.Bundle.main, HamatonBundle.self)
        let lan = HamatonLanguageManager.currentLanguage()
        HamatonLanguageManager.shared.language = lan!
    }
    
    // 加上判断登录逻辑
    private func setRootViewController() {
        if HamatonLoginManager.shared.isLogin() {
            window?.rootViewController = hamatonTabBarControllerStyle()
        } else {
            window?.rootViewController = HamatonLoginViewController()
        }
        window?.makeKeyAndVisible()
    }
    
    private func hamatonTabBarControllerStyle() -> HamatonTabBarController {
        let tabBarController = HamatonTabBarController()
      //  let v1 = HamatonHomeViewController()
        let v2 = HamatonDeviceViewController()
     //   let v3 = HamatonCarManagementViewController()
        let v4 = HamatonMeViewController()
      //  let navigationController1 = HamatonNavigationController.init(rootViewController: v1)
        let navigationController2 = HamatonNavigationController.init(rootViewController: v2)
      //  let navigationController3 = HamatonNavigationController.init(rootViewController: v3)
        let navigationController4 = HamatonNavigationController.init(rootViewController: v4)
      //  v1.tabBarItem = ESTabBarItem.init(TabBarHighlightableContentView(), title: "首页", image: UIImage(named: "home_0"), selectedImage: UIImage(named: "home_1"))
        v2.tabBarItem = ESTabBarItem.init(TabBarHighlightableContentView(), title: "设备", image: UIImage(named: "device_0"), selectedImage: UIImage(named: "device_1"))
       // v3.tabBarItem = ESTabBarItem.init(TabBarHighlightableContentView(), title: "车辆", image: UIImage(named: "car_0"), selectedImage: UIImage(named: "car_1"))
        v4.tabBarItem = ESTabBarItem.init(TabBarHighlightableContentView(), title: "我的", image: UIImage(named: "me_0"), selectedImage: UIImage(named: "me_1"))
        
        tabBarController.viewControllers = [navigationController2, navigationController4]
        return tabBarController
    }

    func resetRootViewController() {
        setRootViewController()
    }
}
